package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.common.validator.ValidatorAdd;
import com.heima.model.common.validator.ValidatorUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value = "频道管理", tags = "频道管理", description = "频道管理API")
@RequestMapping("/api/v1/channel")
public class AdChannelController {
    @Autowired
    private AdChannelService adChannelService;

    //根据名称分页查询频道列表
    @PostMapping("list")
    @ApiOperation("频道分页列表查询")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDTO dto) {
        return adChannelService.findByNameAndPage(dto);
    }

    //频道新增
    @PostMapping("/save")
    @ApiOperation("新增频道")
    public ResponseResult insert(@RequestBody @Validated({ValidatorAdd.class}) AdChannel adChannel) {
        return adChannelService.insert(adChannel);
    }

    //频道修改&频道有效无效设置
    @PostMapping("update")
    @ApiOperation("频道修改&频道有效无效设置")
    public ResponseResult update(@RequestBody @Validated({ValidatorUpdate.class}) AdChannel adChannel) {
        return adChannelService.update(adChannel);
    }

    //频道删除
    @GetMapping("/del/{id}")
    @ApiOperation("频道删除")
    public ResponseResult deleteById(@PathVariable("id") Integer id) {
//        if(true){
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
        return adChannelService.deleteById(id);
    }

    //查询全部频道
    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        return ResponseResult.okResult(adChannelService.list());
    }

    //根据频道ID查询频道信息
    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(adChannelService.getById(id));
    }
}